local luochao = fk.CreateSkill{
  name = "investiture__luochao",
}

Fk:loadTranslationTable{
  ["investiture__luochao"] = "烙朝",
  [":investiture__luochao"] = "弃牌阶段开始时，你可视为对任意名角色使用【火攻】，若你以此法对这些角色均造成伤害，你暗置等同于这些角色数的手牌，此回合增加等量的手牌上限。",

  ["#investiture__luochao-prompt"] = "烙朝：可以视为对任意名角色使用【火攻】",
  ["#investiture__luochao-hide"] = "烙朝：请选择%arg张手牌暗置",

  ["$investiture__luochao1"] = "尔等既欲抱木就火，又何恐血散皮消？",
  ["$investiture__luochao2"] = "先剖其目，再拆其骨，温火焙之，最为受用，哈哈哈哈！",
}

local DIY = require "packages/diy_utility/diy_utility"
local TuntianUtil = require "packages/tuntian_studio/utility"

---@param room Room
---@param player ServerPlayer
---@param tos ServerPlayer[]
local function allSuffered(room, player, tos)
  tos = table.simpleClone(tos)
  local events = room.logic.all_game_events
  for i = #events, room.logic:getCurrentEvent().id, -1 do
    if events[i].event == GameEvent.Damage then
      local d = events[i].data ---@type DamageData
      if d.from == player and d.to and d.card and d.card.skillName == "investiture__luochao" then
        table.removeOne(tos, d.to)
      end
    end
  end

  if #tos == 0 then
    return true
  else
    return false
  end
end

luochao:addEffect(fk.EventPhaseStart, {
  anim_type = "offensive",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill("investiture__luochao") and player == target
    and data.phase == Player.Discard
  end,
  on_cost = function (self, event, target, player, data)
    local fire = Fk:cloneCard("fire_attack")
    fire.skillName = "investiture__luochao"
    local tos = player.room:askToChoosePlayers(player, {
      targets = fire:getAvailableTargets(player),
      min_num = 1,
      max_num = 999,
      skill_name = "investiture__luochao",
      cancelable = true,
      prompt = "#investiture__luochao-prompt",
      no_indicate = true
    })

    if #tos > 0 then
      event:setCostData(self, tos)
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local tos = event:getCostData(self) ---@type ServerPlayer[]
    local fire = Fk:cloneCard("fire_attack")
    fire.skillName = "investiture__luochao"
    room:useCard{
      card = fire,
      from = player,
      tos = tos,
    }

    if allSuffered(room, player, tos) then
      local shown = table.filter(player:getCardIds("h"), function (c) return TuntianUtil.isShown(c) end)
      local to_hide = {}
      if #tos >= #shown then
        to_hide = shown
      else
        to_hide = room:askToCards(player, {
          min_num = #tos,
          max_num = #tos,
          skill_name = "investiture__luochao",
          cancelable = false,
          prompt = "#investiture__luochao-hide:::"..#tos,
          pattern = tostring(Exppattern{ id = shown }),
        })
      end
      DIY.hideCards(player, to_hide)

      if player:isAlive() then
        room:addPlayerMark(player, MarkEnum.AddMaxCardsInTurn, #tos)
      end
    end
  end
})

return luochao